//
//  XFTextField.m
//  WorkManager
//
//  Created by ukongm on 17/2/8.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFTextField.h"
#import "XFManager.h"
/** 导航的高度 */
static CGFloat const NAVI_HEIGHT = 50;
/** 间隔 */
static CGFloat const VIEW_GAP = 10;
@interface XFTextField()<UITextViewDelegate>

/** 输入控件 */
@property(nonatomic,strong)UITextView* textView;
/** 提示文字的label */
@property(nonatomic,strong)UILabel* hintLb;
/** 导航 */
@property(nonatomic,strong)UIView* naviView;
/** 遮罩 */
@property(nonatomic,strong)UIView* backView;
/** 线 */
@property(nonatomic,strong)UIView* lineView;

@end

@implementation XFTextField
{
    UIButton* _fixedBtn; //确定按钮
    UILabel* _titleLb; // 标题
    CGRect _frame; //初始frame
    CGFloat _reusltY; // 弹起之后的Y
    CGFloat _initTextViewHeight; //刚开始 的高度
}

- (UIView *)lineView{
    
    if (_lineView == nil) {
        CGFloat lineWidth = 3;
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(self.textView.x, self.textView.maxY, self.textView.width, lineWidth)];
        
        _lineView.backgroundColor = [UIColor pinkColor];
        
        _lineView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    }
    return _lineView;
}

- (UIView *)backView{
    
    if (_backView == nil) {
        _backView = [[UIView alloc] initWithFrame:CGRectZero];
        _backView.backgroundColor = [UIColor blackColor];
        _backView.alpha = 0;
        [_backView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)]];
    }
    return _backView;
}

- (UIView *)naviView{
    
    if (_naviView == nil) {
        CGFloat gap = VIEW_GAP;
        CGFloat width = 50;
        _naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, NAVI_HEIGHT)];
        _naviView.backgroundColor = [UIColor red:200 green:200 blue:200];
        
        _titleLb = [[UILabel alloc] initWithFrame:_naviView.bounds];
        _titleLb.font = [UIFont systemFontOfSize:15];
        _titleLb.textColor = [UIColor red:30 green:30 blue:30];
        _titleLb.textAlignment = NSTextAlignmentCenter;
        
        UIButton* cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(gap, 0, width, _naviView.height)];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        
        _fixedBtn = [[UIButton alloc] initWithFrame:CGRectMake(_naviView.width - gap - width, 0, width, _naviView.height)];
        _fixedBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [_fixedBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_fixedBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_fixedBtn addTarget:self action:@selector(fixedAction) forControlEvents:UIControlEventTouchUpInside];
        _fixedBtn.enabled = NO;
        
        [_naviView addSubview:_titleLb];
        [_naviView addSubview:cancelBtn];
        [_naviView addSubview:_fixedBtn];
    }
    return _naviView;
}

- (UILabel *)hintLb{
    
    if (_hintLb == nil) {
        _hintLb = [[UILabel alloc] initWithFrame:self.textView.frame];
        [_hintLb changeHeight:25];
        [_hintLb changeY:_hintLb.y + 5];
        [_hintLb changeX:_hintLb.x + 5];
        _hintLb.textColor = [UIColor lightGrayColor];
        _hintLb.font = [UIFont systemFontOfSize:17];
    }
    return _hintLb;
}

- (UITextView *)textView{
    
    if (_textView == nil) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(VIEW_GAP * 1.5, VIEW_GAP/2 + self.naviView.maxY, self.width - 3 * VIEW_GAP, self.naviView.maxY - VIEW_GAP * 1.5)];
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:17];
        _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _textView.backgroundColor = [UIColor clearColor];
        _textView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        _initTextViewHeight = _textView.height;
    }
    return _textView;
}

- (instancetype)init
{
    CGFloat width = self.screenSize.width * 0.9;
    CGFloat height = 105;
    CGFloat x = (self.screenSize.width - width)/2;
    CGFloat y = self.screenSize.height;
    
    _frame = CGRectMake(x, y, width, height);
    
    
    return [self initWithFrame:_frame];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.naviView];
        
        [self addSubview:self.hintLb];
        [self addSubview:self.textView];
        [self addSubview:self.lineView];
        
        [self layerWithRadian:3];
        self.backgroundColor = [UIColor whiteColor];
        
        
    }
    return self;
}

- (void)showInView:(UIView *)view{
    [self showInView:view fixedBlock:self.fixedBlock];
}

- (void)showInView:(UIView *)view fixedBlock:(void (^)(NSString *))fixedBlock{
    
    self.fixedBlock = fixedBlock;
    
    [view addSubview:self.backView];
    [view addSubview:self];
    self.backView.frame = view.bounds;
    [self becomeFirstResponder];
    _reusltY = _frame.origin.y - 300 - self.height - 10;
    [UIView animateWithDuration:0.25 animations:^{
        [self changeY:_reusltY];
        self.backView.alpha = 0.8;
    }];
}

- (void)hide{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = _frame;
        [self resignFirstResponder];
        self.backView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.backView removeFromSuperview];
        [self removeFromSuperview];
    }];
    
}

- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        _hintLb.text = self.placeholder;
        _fixedBtn.enabled = NO;
        [_fixedBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }else{
        [_fixedBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _fixedBtn.enabled = YES;
        
        _hintLb.text = @"";
        
        [self mathForTextView:textView];
    }
}

- (void)setText:(NSString *)text{
    if (text.length > 0) {
        self.hintLb.text = @"";
    }
    self.textView.text = text;
}

- (NSString *)text{
    return self.textView.text;
}

- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    if (self.textView.text.length == 0) {
        self.hintLb.text = placeholder;
    }
}

- (void)setTitle:(NSString *)title{
    _titleLb.text = title;
}

- (NSString *)title{
    return _titleLb.text;
}

- (CGFloat)maxHeight{
    if (_maxHeight == 0) {
        return 70;
    }
    return _maxHeight;
}

- (void)becomeFirstResponder{
    [self.textView becomeFirstResponder];
}

- (void)resignFirstResponder{
    [self.textView resignFirstResponder];
}

#pragma mark 核心计算
- (void)mathForTextView:(UITextView*)textView{
    //计算高度
    CGSize size = XFMathSizeForString(textView.text, CGSizeMake(textView.width, CGFLOAT_MAX), textView.font);
    
    //计算和一开比较的差值，高度差值
    CGFloat scale = size.height - _initTextViewHeight;
    //计算整个控件的高度
//    size.height = textView.maxY + (size.height - textView.height) + VIEW_GAP * 2.5;
//    
    //如果没有一开始的大，就归0
    if (size.height <= _initTextViewHeight) {
        scale = 0;
    }
//
    if (scale > self.maxHeight) {
        scale = self.maxHeight;
    }
    
    [self changeHeight:_frame.size.height + scale];
    [self changeY:_reusltY - scale];

}

//确定
- (void)fixedAction{
    
    if (self.fixedBlock) {
        self.fixedBlock(self.textView.text);
    }
    
    self.textView.text = @"";
    _hintLb.text = self.placeholder;
    _fixedBtn.enabled = NO;
    [_fixedBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    
    [self hide];
}


@end
